package com.example.demo.utils;

import org.apache.commons.collections4.CollectionUtils;

import java.util.*;

/**
 * @author Liunh
 */
public class CollectionUtil {

    private int everyListCountDefault = 1000;

    private CollectionUtil() {
    }

    private static volatile CollectionUtil instance = null;

    public static CollectionUtil getInstance() {
        if (null == instance) {
            synchronized (CollectionUtil.class) {
                if (null == instance) {
                    instance = new CollectionUtil();
                }
            }
        }
        return instance;
    }


    public <T> Map<Integer, List<T>> toMultiList(List<T> list, int everyCount) {
        if (everyCount < everyListCountDefault) {
            everyCount = everyListCountDefault;
        }
        Map<Integer, List<T>> listMap = Collections.emptyMap();
        if (CollectionUtils.isNotEmpty(list)) {
            int listSize = list.size();
            if (everyCount > listSize) {
                everyCount = listSize;
            }
            int listMultiCount = 1;
            int mod = listSize % everyCount;
            if (mod == 0) {
                listMultiCount = listSize / everyCount;
            } else {
                listMultiCount = (listSize / everyCount) + 1;
            }
            listMap = new HashMap<>(listMultiCount);
            for (int i = 0; i < listMultiCount; i++) {
                final List<T> listTmp = new ArrayList<>();
                for (int x = 0; x < everyCount; x++) {
                    listTmp.add(list.get(x));
                    listMap.put(i + 1, listTmp);
                }
            }
        }
        return listMap;
    }


}
